package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.TMStatMfdCriteriaMapper;
import cc.rengu.igas.momp.common.dao.impl.TMStatMfdCriteriaMapperImpl;
import cc.rengu.igas.momp.common.entity.TMStatMfdCriteria;
import cc.rengu.igas.momp.common.enums.QueryTypeEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.request.EditMchntFeeTxnAmtLimitRequest;
import cc.rengu.igas.momp.facade.response.EditMchntFeeTxnAmtLimitResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.dbs.Database;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 商户当前周期手续费优惠限额修改
 *
 * @author liujinan
 * @version 1.0.0
 * @date 2021/6/21 09:19
 */
public class EditMchntFeeTxnAmtLimitService extends RadpService {
    /**
     * 法人机构号
     */
    private String globalInstId;
    /**
     * 操作类型
     */
    private String globalQueryType;
    /**
     * 操作人
     */
    private String globalQueryId;
    /**
     * 操作员所属法人机构
     */
    private String globalQueryOrgId;
    /**
     * 操作角色
     */
    private String globalQueryRole;
    /**
     * 当前系统日期
     */
    private String globalTimeStamp;
    /**
     * 商户参与营销活动编号
     */
    private String globalMchntNo;
    /**
     * 营销活动商户名称
     */
    private String globalMcNo;
    /**
     * 商户优惠额度
     */
    private String mchntFeeTxnAmtLimit;
    /**
     * 优惠额度结束日期
     */
    private String txnAmtLimitEndDate;
    /**
     * 交易日期
     */
    private String globalTxnDate;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            EditMchntFeeTxnAmtLimitRequest editMchntFeeTxnAmtLimitRequest = new EditMchntFeeTxnAmtLimitRequest();
            ConvertUtil.convertOutput(editMchntFeeTxnAmtLimitRequest);

            EditMchntFeeTxnAmtLimitResponse editMchntFeeTxnAmtLimitResponse = new EditMchntFeeTxnAmtLimitResponse();
            editMchntFeeTxnAmtLimitResponse.setHeader(editMchntFeeTxnAmtLimitRequest.getHeader());
            BizResponse<EditMchntFeeTxnAmtLimitResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(editMchntFeeTxnAmtLimitResponse);

            //报文检查
            messageValidation(editMchntFeeTxnAmtLimitRequest);

            // 服务调用
            BizResponse<EditMchntFeeTxnAmtLimitResponse> bizResponseNew = editMchntFeeTxnAmtLimit(xmlTreeUtil, editMchntFeeTxnAmtLimitRequest, editMchntFeeTxnAmtLimitResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 商户当前周期手续费优惠限额修改
     *
     * @param editMchntFeeTxnAmtLimitRequest  商户当前周期手续费优惠限额修改接口请求
     * @param editMchntFeeTxnAmtLimitResponse 商户当前周期手续费优惠限额修改接口应答
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.EditMchntFeeTxnAmtLimitResponse>商户当前周期手续费优惠限额修改接口应答报文
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/5/28 14:50
     */
    private BizResponse<EditMchntFeeTxnAmtLimitResponse> editMchntFeeTxnAmtLimit(XmlTreeUtil xmlTreeUtil, EditMchntFeeTxnAmtLimitRequest editMchntFeeTxnAmtLimitRequest, EditMchntFeeTxnAmtLimitResponse editMchntFeeTxnAmtLimitResponse) throws Exception {

        BizResponse<EditMchntFeeTxnAmtLimitResponse> bizResponse = new BizResponse<>();

        //银行人员查询
        if (QueryTypeEnum.BANK_QUERY_TYPE.getQueryTypeCode().equals(globalQueryType)) {
            rglog.info("银行人员开始修改商户当前周期手续费优惠限额,商户<{}> ", globalMchntNo);

            TMStatMfdCriteria tmStatMfdCriteria = new TMStatMfdCriteria();
            tmStatMfdCriteria.setInstId(globalInstId);
            tmStatMfdCriteria.setMcNo(globalMcNo);
            tmStatMfdCriteria.setMchntNo(globalMchntNo);
            tmStatMfdCriteria.setPeriodStartDate(globalTxnDate);
            tmStatMfdCriteria.setPeriodEndDate(globalTxnDate);
            //将要修改的额度值
            tmStatMfdCriteria.setMrctDisUpLmtVal(mchntFeeTxnAmtLimit);
            //将要修改的有效日期值
            if (!StringUtil.isNullorEmpty(txnAmtLimitEndDate)) {
                tmStatMfdCriteria.setMrctDisUplmtEffDt(txnAmtLimitEndDate);
                tmStatMfdCriteria.setPeriodStartDate(txnAmtLimitEndDate);
            }
            tmStatMfdCriteria.setUpdateTime(globalTimeStamp);
            tmStatMfdCriteria.setExitDiscountStatus(CommonConstant.MC_NOT_EXIT_FLAG);

            /* 开启数据库事务 */
            String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
            }
            DbsUtil dbsUtil = new DbsUtil(dbPoolName);
            dbsUtil.dbsBeginTransaction();

            TMStatMfdCriteriaMapper tmStatMfdCriteriaMapper = new TMStatMfdCriteriaMapperImpl();
            int returnCode = tmStatMfdCriteriaMapper.updateMchntFeeTxnAmtLimit(tmStatMfdCriteria);
            if (Database.DBS_SUCCESS != returnCode) {
                rglog.error("银行人员修改商户当前周期手续费优惠限额更新手续费达标统计表数据失败！RETURN_CODE=<{}>", returnCode);
                dbsUtil.dbsEndTransaction(false);
                throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }

            dbsUtil.dbsEndTransaction(true);

            rglog.info("银行人员修改商户当前周期手续费优惠限额完成,商户<{}> ", globalMchntNo);
        } else {
            /* 其他查询角色不支持 */
            rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
            throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
        }

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        editMchntFeeTxnAmtLimitResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        editMchntFeeTxnAmtLimitResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(editMchntFeeTxnAmtLimitResponse);

        return bizResponse;
    }

    /**
     * 请求报文体内容检查
     *
     * @param editMchntFeeTxnAmtLimitRequest 商户当前周期手续费优惠限额修改接口请求对象
     * @author liujinan
     * @date 2021/6/23 09:55
     */
    private void messageValidation(EditMchntFeeTxnAmtLimitRequest editMchntFeeTxnAmtLimitRequest) {

        if (StringUtil.isNullorEmpty(editMchntFeeTxnAmtLimitRequest.getHeader().getTransDate())) {
            rglog.error("交易日期必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalTxnDate = editMchntFeeTxnAmtLimitRequest.getHeader().getTransDate();
        }

        if (StringUtil.isNullorEmpty(editMchntFeeTxnAmtLimitRequest.getHeader().getTransTime())) {
            rglog.error("交易时间必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(editMchntFeeTxnAmtLimitRequest.getHeader().getInstId())) {
            rglog.error("法人机构号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalInstId = editMchntFeeTxnAmtLimitRequest.getHeader().getInstId();
        }

        if (StringUtil.isNullorEmpty(editMchntFeeTxnAmtLimitRequest.getHeader().getTraceNo())) {
            rglog.error("流水号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(editMchntFeeTxnAmtLimitRequest.getQueryType())) {
            rglog.error("操作员类型为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryType = editMchntFeeTxnAmtLimitRequest.getQueryType();
        }

        if (StringUtil.isNullorEmpty(editMchntFeeTxnAmtLimitRequest.getQueryId())) {
            rglog.error("操作员ID为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryId = editMchntFeeTxnAmtLimitRequest.getQueryId();
        }

        if (StringUtil.isNullorEmpty(editMchntFeeTxnAmtLimitRequest.getQueryRole())) {
            rglog.error("操作员角色为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryRole = editMchntFeeTxnAmtLimitRequest.getQueryRole();
        }

        if (StringUtil.isNullorEmpty(editMchntFeeTxnAmtLimitRequest.getQueryOrgId())) {
            rglog.error("操作员所属法人机构为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryOrgId = editMchntFeeTxnAmtLimitRequest.getQueryOrgId();
        }

        if (StringUtil.isNullorEmpty(editMchntFeeTxnAmtLimitRequest.getMchntNo())) {
            rglog.error("商户号为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalMchntNo = editMchntFeeTxnAmtLimitRequest.getMchntNo();
        }

        if (StringUtil.isNullorEmpty(editMchntFeeTxnAmtLimitRequest.getMcNo())) {
            rglog.error("商户参与营销活动编号为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalMcNo = editMchntFeeTxnAmtLimitRequest.getMcNo();
        }

        if (StringUtil.isNullorEmpty(editMchntFeeTxnAmtLimitRequest.getMchntFeeTxnAmtLimit())) {
            rglog.error("商户优惠额度为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            mchntFeeTxnAmtLimit = editMchntFeeTxnAmtLimitRequest.getMchntFeeTxnAmtLimit();
        }

        txnAmtLimitEndDate = editMchntFeeTxnAmtLimitRequest.getTxnAmtLimitEndDate();

        globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
    }
}
